package org.vaadin.example.views.helloworld;


import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Menu;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.example.security.CustomUserDetailsService;
import org.vaadin.example.security.SecurityService;
import org.vaadin.example.util.I18nUtil;
import org.vaadin.example.views.MainLayout;
import org.vaadin.lineawesome.LineAwesomeIconUrl;

import java.util.Locale;

//@PageTitle("Hello World")
//@Route(value = "/HelloWorldView",layout = MainLayout.class)
//@AnonymousAllowed
@PermitAll
@PageTitle("HelloWorldPage")
@Route(value = "hello", layout = MainLayout.class)
@RouteAlias(value = "", layout = MainLayout.class)  //必须配置1个空路由，用于security登录成功后进入这个页面

@Menu(order = 0, icon = LineAwesomeIconUrl.GLOBE_SOLID)
public class HelloWorldView extends HorizontalLayout {

    private static final Logger log = LoggerFactory.getLogger(HelloWorldView.class);

    private final SecurityService securityService;


    private TextField name;
    private Button sayHello;
//    private Locale browserLocale ;

    public HelloWorldView(HttpServletRequest request, SecurityService securityService) {
        this.securityService = securityService;

        String traceId = getTraceId();
//        log.info("Processing request with traceId: {}", traceId);

        String u = securityService.getAuthenticatedUser().getUsername();
        log.info("current login User name: {},traceId: {}", u,traceId);

        String cokStr = null;
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if ("myCookie".equals(cookie.getName())) {
//                    System.out.println("Cookie Value: " + cookie.getValue());
//                    cokStr = cookie.getValue();
//                }
//            }
//        }
//
//        if (cokStr==null){
//            browserLocale = VaadinRequest.getCurrent().getLocale();
//        }else {
//            browserLocale = new Locale(cokStr);
//        }


        name = new TextField("Your name");
        sayHello = new Button("Say hello");
        sayHello.addClickListener(e -> {
            Notification.show("Hello " + name.getValue());
        });
        sayHello.addClickShortcut(Key.ENTER);

        setMargin(true);
        setVerticalComponentAlignment(Alignment.END, name, sayHello);

//        UI.getCurrent().getPage().setTitle(I18nUtil.getMessage("HelloWorldPage",browserLocale));

        add(name, sayHello);
    }

    public String getTraceId() {
        return (String) UI.getCurrent().getSession().getAttribute("traceId");
    }
}

